package com.school.test.controller;

import com.school.common.core.utils.StringUtils;
import com.school.common.core.web.controller.BaseController;
import com.school.common.core.web.domain.AjaxResult;
import com.school.common.core.web.page.TableDataInfo;
import com.school.common.log.annotation.Log;
import com.school.common.log.enums.BusinessType;
import com.school.test.domain.StatisticsVisit;
import com.school.test.domain.Websiteinfo;
import com.school.test.domain.vo.ParamsVo;
import com.school.test.service.StatisticsVisitService;
import com.school.test.service.WebsiteinfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/websiteInfo")
public class WebsiteInfoController extends BaseController {

    @Autowired
    private WebsiteinfoService websiteinfoService;

    @PostMapping("/list")
    @ApiOperation(value="查询",notes = "网址信息查询")
    public TableDataInfo list(@RequestBody ParamsVo paramsVo)
    {
        startPage();
        List<Websiteinfo> list = websiteinfoService.selectInfo(paramsVo);
        return getDataTable(list);
    }



    /**
     * 统计新增
     */
    @Log(title = "网址新增", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value="网址新增",notes = "新增统计，一个公司type 同一个源的数据只会有一个")
    public AjaxResult add(@Validated @RequestBody Websiteinfo websiteinfo)
    {
        //限制一天只能提交一次当天的记录
       List<Websiteinfo> websiteinfoList =  websiteinfoService.selectInfoByCompanyType(websiteinfo.getType(),websiteinfo.getSource());
       if(websiteinfoList.size() > 0){
           return error("添加网站信息'" + "'失败，该公司下已经存在同 "+ websiteinfo.getSource() +"的数据，请确认");
       }
        return toAjax(websiteinfoService.addWebsiteinfo(websiteinfo));
    }


    /**
     * 统计修改
     */
    @Log(title = "统计修改", businessType = BusinessType.INSERT)
    @PostMapping("/update")
    @ApiOperation(value="网址修改",notes = "网址修改")
    public AjaxResult update(@Validated @RequestBody Websiteinfo websiteinfo)
    {
        //通过ID 查询 统计信息。
        Websiteinfo web = websiteinfoService.selectInfoById(websiteinfo.getId());
        if(null == web){
            return error("通过ID查询数据为空，请确认");
        }

        //限制一天只能提交一次当天的记录
        List<Websiteinfo> websiteinfoList =  websiteinfoService.selectInfoByCompanyType(websiteinfo.getType(),websiteinfo.getSource());
        if(websiteinfoList.size() > 0){
            return error("添加网站信息'" + "'失败，该公司下已经存在同 "+ websiteinfo.getSource() +"的数据，请确认");
        }
        if(null == websiteinfo.getId()){
            websiteinfo.setId(web.getId());
        }
        if(StringUtils.isEmpty(websiteinfo.getType())){
            websiteinfo.setType(web.getType());
        }
        if(StringUtils.isEmpty(websiteinfo.getSource())){
            websiteinfo.setSource(web.getSource());
        }
        if(StringUtils.isEmpty(websiteinfo.getAddress())){
            websiteinfo.setAddress(web.getAddress());
        }
        return toAjax(websiteinfoService.updateWebsiteinfo(websiteinfo));
    }


    @Log(title = "地址删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{id}")
    @ApiOperation(value="地址删除",notes = "删除地址数据，物理删除。")
    public AjaxResult remove(@PathVariable String id)
    {
        int num = websiteinfoService.deleteWebsiteInfoByIds(id);
        return success(num + "条数据删除成功");
    }


}
